// Pinia store configuration

import { createPinia } from 'pinia'
import type { App } from 'vue'

// Create pinia instance
export const pinia = createPinia()

// Plugin to add store initialization
pinia.use(({ store }) => {
  // Add initialize method to all stores if they have one
  if (typeof store.initialize === 'function') {
    store.initialize()
  }
  
  // Add cleanup method to all stores if they have one
  if (typeof store.cleanup === 'function') {
    // Store cleanup function for later use
    store._cleanup = store.cleanup
  }
})

// Install pinia in Vue app
export function setupPinia(app: App) {
  app.use(pinia)
}

// Global store cleanup function
export function cleanupStores() {
  // Access stores through pinia's internal state
  const stores = (pinia as any)._s
  if (stores) {
    stores.forEach((store: any) => {
      if (typeof store._cleanup === 'function') {
        store._cleanup()
      }
    })
  }
}

export default pinia